package org.jeecg.modules.inOrderPt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.modules.basicSkuPt.entity.BasicSkuPt;
import org.jeecg.modules.basicSkuPt.service.IBasicSkuPtService;
import org.jeecg.modules.inOrderPt.entity.InOrderPt;
import org.jeecg.modules.inOrderPt.entity.InOrderZiPt;
import org.jeecg.modules.inOrderPt.mapper.InOrderZiPtMapper;
import org.jeecg.modules.inOrderPt.mapper.InOrderPtMapper;
import org.jeecg.modules.inOrderPt.service.IInOrderPtService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 入库单主表pt品
 * @Author: jeecg-boot
 * @Date:   2024-07-24
 * @Version: V1.0
 */
@Service
public class InOrderPtServiceImpl extends ServiceImpl<InOrderPtMapper, InOrderPt> implements IInOrderPtService {

	@Autowired
	private InOrderPtMapper inOrderPtMapper;
	@Autowired
	private InOrderZiPtMapper inOrderZiPtMapper;
	@Autowired
	private IBasicSkuPtService basicSkuPtService;
	@Override
	@Transactional
	public void saveMain(InOrderPt inOrderPt, List<InOrderZiPt> inOrderZiPtList) {
		inOrderPtMapper.insert(inOrderPt);
		if(inOrderZiPtList!=null && inOrderZiPtList.size()>0) {
			for(InOrderZiPt entity:inOrderZiPtList) {
				BigDecimal numberBigDeimal = new BigDecimal(entity.getNumber());
				LambdaQueryWrapper<BasicSkuPt> eq = new LambdaQueryWrapper<BasicSkuPt>();
				if (entity.getCustomerSkuCode()!=null&& !entity.getCustomerSkuCode().isEmpty()){
					eq.eq(BasicSkuPt::getCustomerSkuCode, entity.getCustomerSkuCode());
				}else{
					eq.eq(BasicSkuPt::getSkuCode, entity.getSkuCode());
				}
				BasicSkuPt one = basicSkuPtService.list(eq).get(0);
				if (one != null) {
					if(one.getSpecificationsNum()!=null){
						BigDecimal specificationsNumBigDeimal = new BigDecimal(one.getSpecificationsNum().toString());
						BigDecimal divide = numberBigDeimal.divide(specificationsNumBigDeimal, 2, RoundingMode.HALF_UP);
						entity.setBoxNum(divide.doubleValue());
					}
                }
				//外键设置
				entity.setInOrderId(inOrderPt.getId());
				inOrderZiPtMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(InOrderPt inOrderPt,List<InOrderZiPt> inOrderZiPtList) {
		inOrderPtMapper.updateById(inOrderPt);
		
		//1.先删除子表数据
		inOrderZiPtMapper.deleteByMainId(inOrderPt.getId());
		
		//2.子表数据重新插入
		if(inOrderZiPtList!=null && inOrderZiPtList.size()>0) {
			for(InOrderZiPt entity:inOrderZiPtList) {
				//外键设置
				entity.setInOrderId(inOrderPt.getId());
				inOrderZiPtMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		inOrderZiPtMapper.deleteByMainId(id);
		inOrderPtMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			inOrderZiPtMapper.deleteByMainId(id.toString());
			inOrderPtMapper.deleteById(id);
		}
	}

	@Override
	public List<InOrderPt> getByYear(String year, String month) {
		return this.baseMapper.getByYear(year,month);
	}

}
